# from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import tornado.web
import tornado.websocket
import tornado.ioloop
import cgi
def run():
	try:
		# websocketServer = server("0.0.0.0", 8081, 1000)
		server = HTTPServer(('', 8080), HTTPorAPPserver)
		print 'Started httpserver on port ' , 8080
		server.serve_forever()
	 
	except KeyboardInterrupt:
		print '^C received, shutting down the web server'
		server.socket.close()

class HTTPorAPPserver(BaseHTTPRequestHandler):
	def do_GET(self):
		self.send_response(200)
		self.send_header('Content-type','text/html')
		self.end_headers()
		cwd = __file__.split('/')
		cwd.pop()
		cwd = '/'.join(cwd)
		r = open(cwd + '/interface.html', 'r')
		self.wfile.write(r.read())
		r.close()
		return

	def do_POST(self):
		self.send_response(302)
		self.send_header('Location', '/')
		self.end_headers()
		ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
		if ctype == 'multipart/form-data':
			postvars = cgi.parse_multipart(self.rfile, pdict)
		elif ctype == 'application/x-www-form-urlencoded':
			length = int(self.headers.getheader('content-length'))
			postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
		else:
			postvars = {}
		# print "Yolo: " + str(postvars['keywords'][0])
		hook.main.playSong(postvars['keywords'][0])